#!/bin/bash
. "$(dirname $0)/utils.sh"
run-safe

# You might want to adjust these.
ZONE=America/New_York
POOL=pool.ntp.org

LOG Installing ntp using $POOL for $ZONE
opkg update
opkg install ntp
rm -f /etc/localtime ;  ln -s /usr/share/zoneinfo/$ZONE /etc/localtime
LOG Set the time
ntpdate -b -s -u pool.ntp.org
LOG "$( date )"

LOG Configure the ntp deamons
cat <<EOF > /etc/ntp.conf
# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /etc/ntp.drift
# This obtains a random server which will be close
# (in IP terms) to the machine.  Add other servers
# as required, or change this.
server 0.${POOL}
server 1.${POOL}
server 2.${POOL}
server 3.${POOL}
# Using local hardware clock as fallback
# Disable this when using ntpd -q -g -x as ntpdate or it will sync to itself
#server 127.127.1.0
#fudge 127.127.1.0 stratum 14
# Defining a default security setting
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
EOF

LOG Start the ntp deamons
systemctl enable ntpdate.service
systemctl enable ntpd.service
